package com.mkaech.eece411;

import java.io.*;
import java.rmi.*;
import java.util.Queue;

public class Main {
    
    static GUI gui;
    static MessageQueue _queue;
    static MessageQueue RemoteQueue;
    
    public static void main(String[] args) {
        // create a shared buffer where the GUI add the messages thet need to 
        // be sent out by the main thread.  The main thread stays in a loop 
        // and when a new message shows up in the buffer it sends it out 
        // to the chat server (using RMI)
        _queue = new MessageQueue();
        RemoteQueue = new MessageQueue();
        
        // instantiate the GUI - in a new thread
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                gui = GUI.createAndShowGUI(_queue);  
            }
        });
        
        
        // hack make sure the GUI instantioation is completed by the GUI thread 
        // before the next call
        while (gui == null)
            Thread.currentThread().yield();
        
       
        
        // The code below serves as an example to show how the shares message 
        // between the GUI and the main thread.
        // You will probably want to replace the code below with code that sits in a loop,  
        // waits for new messages to be entered by the user, and sends them to the 
        // chat server (using an RMI call)
        // 
        // In addition you may want to add code that
        //   * connects to the chat server and provides an object for callbacks (so 
        //     that the server has a way to send messages generated by other users)
        //   * implement the callback object which is called by the server remotely 
        //     and, in turn, updates the local GUI
        
        // Get the user to provide their name
        gui.addToTextArea("Please enter your name to begin...");
        String name;
        while (true)
        {
            try
            {
            	// Process inputs and send to server
            	if(!_queue.isEmpty())
            	{
            		name = _queue.dequeue();
            		break;
            	}            	
            	Thread.sleep(100);
            }
            catch (InterruptedException ie)
            {
                return;
            }
        }
        
        // Create our remote object
        ChatClient client;
        try
        {
        	client = new ChatClient(RemoteQueue);
        }
        catch(Exception e)
        {
        	System.out.println("ChatClient failed: " + e);
        	return;
        }
        
        // Register our remote object with the chat server
        IChatClientRegistry clientRegistry;
        try
        {
            clientRegistry = (IChatClientRegistry)Naming.lookup ("//localhost/SChatClientRegistry");
            clientRegistry.Register(client);
        }
        catch (Exception e)
        {
        	System.out.println ("clientRegistry failed: " + e);
        	return;
        }

        gui.clearTextArea();
        try
        {
        	clientRegistry.SendMessageToServer(name + " has connected.");
        }
        catch(RemoteException re)
        {
        	System.out.println("Error sending message to server: " + re);
        }
        
        while (true)
        {
            try
            {
            	// Process inputs and send to server
            	if(!_queue.isEmpty())
            	{
            		clientRegistry.SendMessageToServer(name + ": " + _queue.dequeue());
            	}
            	
            	// Process server messages and display in GUI
            	if(!RemoteQueue.isEmpty())
            	{
            		gui.addToTextArea(RemoteQueue.dequeue());
            	}
            	
            	Thread.sleep(100);
            }
            catch (InterruptedException ie)
            {
                break;
            }
            catch(RemoteException re)
            {
            	System.out.println("Error sending message to server: " + re);
            }
        }
        
        try
        {
        	clientRegistry.Unregister(client);
        }
        catch(RemoteException re)
        {
        	System.out.println("Error unregistering: " + re);
        }
    }   
}
